package client.game;

import model.Message;
import model.ServerButton;
import client.ClientController;

public class Game {
	private ClientController controller;
	private ClientGameFrame frame;
	private String symbol;
	private GameStateInterface gameState;
	
	public Game(ClientController controller, String opponent, boolean myTurn) {
		this.controller=controller;
		if(myTurn) {
			symbol="X";
			gameState=new MyTurn();
		} else {
			symbol="O";
			gameState=new OpponentsTurn();
		}
		frame=new ClientGameFrame(this, opponent);
		gameState.setStatus();
	}
	public class MyTurn implements GameStateInterface {

		@Override
		public void setStatus() {
			frame.myTurn();
		}

		@Override
		public boolean canPress() {
			return true;
		}
	}
	public class OpponentsTurn implements GameStateInterface {

		@Override
		public void setStatus() {
			frame.opponentsTurn();
		}

		@Override
		public boolean canPress() {
			return false;
		}
	}
	public class GameOver implements GameStateInterface {

		@Override
		public void setStatus() {
			frame.gameOver();
		}

		@Override
		public boolean canPress() {
			return false;
		}
	}
	public void chatFrameClose() {
		controller.chatFrameClose();
	}
	public void sendMessage(String text) {
		controller.sendMessage(text);
	}
	public void buttonPressed(TicTacToeButton button) {
		ServerButton newButton=button.makeServerButton();
		newButton.setText(symbol);
		controller.buttonPressed(newButton);
		gameState=new OpponentsTurn();
		gameState.setStatus();
	}
	public void userExit() {
		frame.userExit();
	}
	public void showMessage(Message message) {
		frame.showMessage(message);
	}
	public void setButton(ServerButton button) {
		frame.setButton(button);
		if(!button.getSymbol().equals(symbol))
			gameState=new MyTurn();
		gameState.setStatus();
	}
	public void showInfo(String info) {
		gameState=new GameOver();
		gameState.setStatus();
		frame.showInfo(info);
	}
	public void restart() {
		controller.restart();
	}
	public boolean canPress() {
		return gameState.canPress();
	}
	public void resetButtons(boolean myTurn) {
		frame.resetButtons();
		if(myTurn)
			gameState=new MyTurn();
		else
			gameState=new OpponentsTurn();
		gameState.setStatus();
	}
}
